/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.service.plugin;

import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.portal.common.PortalPlugin;
import com.apache.portal.common.annotion.AnntationBean;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.rpc.common.LoadRpcService;
import com.apache.tools.DateUtils;
import com.apache.uct.common.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * description: bank对接插件
 *
 * @author Hou Dayu 创建时间：2016-9-17
 */
@AnntationBean(name = "bank")
public class BankSystemPluginImpl implements PortalPlugin {

    private Logger log = LoggerFactory.getLogger(BankSystemPluginImpl.class);

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     */
    public Object doInvoke(HttpServletRequest request, HttpServletResponse response,
            Map<String, String> params) {
        String doCode = params.get("doCode");
        ResultMsg msg = null;
        Map<String, Object> paramsMap = new HashMap();
        paramsMap.putAll(params);
        LoginUser loginUser = PortalPubFactory.getInstance().getLoginUser(request);
        if (loginUser != null) {
            paramsMap.put("userId", loginUser.getUserId());
        }
        //客户发起开户
        if ("BANK_CNP_T000001".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //对公账户绑定
        if ("BANK_CNP_T000002".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //对私银行卡在线绑定
        if ("BANK_CNP_T000003".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //平台发起客户提现
        if ("BANK_CNP_T000008".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //客户发起销户
        if ("BANK_CNP_T000011".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //客户发起银行卡解除绑定
        if ("BANK_CNP_T000017".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //平台日终交易
        if ("BANK_CNP_T000022".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //进入管理页面
        if ("BANK_CNP_T000025".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //平台文件生成通知
        if ("BANK_CNP_N000004".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //账户信息查询
        if ("BANK_CNP_Q000001".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //资金明细查询
        if ("BANK_CNP_Q000002".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //银行卡绑定信息查询
        if ("BANK_CNP_Q000003".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //商户余额查询
        if ("BANK_CNP_Q000005".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //费用查询
        if ("BANK_CNP_Q000006".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //商户流水明细查询
        if ("BANK_CNP_Q000007".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //调用银行--文件下载
        if ("BANK_CNP_F000001".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //本地--生成账户余额文件
        if ("BANK_CNP_FundBala".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //本地--生成余额变动文件
        if ("BANK_CNP_BalaChg".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        //本地--出入金明细对账
        if ("BANK_CNP_DZ".equals(doCode)) {
            return CNP_Method(doCode, request, paramsMap);
        }
        return msg;
    }

    private ResultMsg CNP_Method(String doCode, HttpServletRequest request,
            Map<String, Object> params) {
        ResultMsg msg = new ResultMsg("F", "操作失败！");
        params.put("createTime", DateUtils.Now.fmt_yyyyMMdd_HHmmss());
        ResultEntity entity = LoadRpcService.service().doServiceClient("sbmService", doCode, params,
                PortalPubFactory.getInstance().getRpcInfo("sbm"));
        if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
            msg = new ResultMsg("T", entity.getMessage());
            LoginUser loginUser = (LoginUser) request.getSession().getAttribute("loginUser");
        } else {
            msg = new ResultMsg("F", entity.getMessage());
        }
        return msg;
    }

}
